﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;

namespace ECommerce.Administration.Controls
{
	public partial class SelectTheProductCategories : AdminBaseUsercontrol
	{
		private int _ProductID;
		private Service.ICategoryService _CategoryService;
		private Service.IProductService _ProductService;

		public int ProductID
		{
			get
			{
				if (ViewState["ProductID"] != null)
				{
					_ProductID = (int)ViewState["ProductID"];

					return _ProductID;
				}

				_ProductID = CommonHelper.QueryStringInt("productid");

				return _ProductID;
			}
			set
			{
				ViewState["ProductID"] = value;
				_ProductID = value;
			}
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			BindData();

			SetTheProductCategory();
		}

		protected void GridCategories_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.Header)
			{
				e.Row.Cells[1].Text = GetLocalResourceString("Admin.CateogryList.CategoryName");
				e.Row.Cells[2].Text = GetLocalResourceString("Admin.CateogryList.Description");
				e.Row.Cells[3].Text = GetLocalResourceString("Admin.CateogryList.DisplayOrder");
			}

			if (e.Row.DataItem != null)
			{
				var item = e.Row.DataItem as Data.Entity.Category;

				if (item.ParentCategoryID > 0)
				{
					if (_CategoryService == null)
					{
						_CategoryService = new Service.CategoryService();
					}

					var parentCategory = _CategoryService.GetCategoryByID(item.ParentCategoryID);

					e.Row.Cells[1].Text = parentCategory.CategoryName + "&nbsp;&nbsp;>>&nbsp;&nbsp;" + item.CategoryName;
				}
			}
		}

		private void BindData()
		{
			if (SiteContext.Current == null)
			{
				Server.Transfer("~Administration/AdminLogin.aspx");
			}

			if (_CategoryService == null)
			{
				_CategoryService = new Service.CategoryService();
			}

			var languageID = 0;
			var lang = SiteContext.Current.WorkingLanguage;

			if (lang != null)
			{
				languageID = lang.LanguageId;

				var categoryByLang = _CategoryService.GetCategoryByLanguage(languageID);
				if (categoryByLang != null)
				{
					var categories = categoryByLang.Where(e => e.Published == true);

					GridCategories.DataSource = categories;
					GridCategories.DataBind();
				}
				else
				{
					GridCategories.DataSource = null;
					GridCategories.DataBind();
				}
			}
		}

		private void SetTheProductCategory()
		{
			var productID = CommonHelper.QueryStringInt("productid");

			if (productID > 0)
			{
				if (GridCategories.Rows.Count > 0)
				{
					if (_ProductService == null)
					{
						_ProductService = new Service.ProductService();
					}

					var pcms = _ProductService.GetProduct_Category_MappingsByProduct(productID);

					if (pcms != null)
					{
						var prod_Category_Mappings = (from p in pcms
													  select new
													  {
														  CategoryID = p.CategoryID,
														  DisplayOrder = p.DisplayOrder
													  });

						var categories = prod_Category_Mappings.Select(e => e.CategoryID);

						foreach (GridViewRow row in GridCategories.Rows)
						{
							var cbProductCategory = row.FindControl("cbProductCategory") as CheckBox;
							var hfProductCategoryId = row.FindControl("hfProductCategoryId") as HiddenField;
							var TextDisplayOrder = row.FindControl("TextDisplayOrder") as TextBox;

							int currentCategoryID = 0;
							if (int.TryParse(hfProductCategoryId.Value, out currentCategoryID))
							{
								var isChecked = categories.Contains(currentCategoryID);

								cbProductCategory.Checked = isChecked;

								if (isChecked)
								{
									var displayOrder = prod_Category_Mappings.Where(e => e.CategoryID.Equals(currentCategoryID)).Select(e => e.DisplayOrder).FirstOrDefault();
									TextDisplayOrder.Text = displayOrder.ToString();
								}
							}
						}
					}
				}
			}
		}

		private IEnumerable<int> GetTheProduct_Categories()
		{
			if (ProductID <= 0)
			{
				return null;
			}

			if (_ProductService == null)
			{
				_ProductService = new Service.ProductService();
			}

			var pcm = _ProductService.GetProduct_Category_MappingsByProduct(ProductID);

			if (pcm == null)
			{
				return null;
			}

			return (from c in pcm
					select c.CategoryID);
		}

		public int SaveSelectedProductCategory()
		{
			var lstProductCategories = GetTheProduct_Categories();

			if (_ProductService == null)
			{
				_ProductService = new Service.ProductService();
			}

			foreach (GridViewRow row in GridCategories.Rows)
			{
				var cbProductCategory = row.FindControl("cbProductCategory") as CheckBox;
				var hfProductCategoryId = row.FindControl("hfProductCategoryId") as HiddenField;
				var TextDisplayOrder = row.FindControl("TextDisplayOrder") as TextBox;

				int currentCategoryID = int.Parse(hfProductCategoryId.Value);

				if (cbProductCategory.Checked)
				{
					Data.Entity.Product_Category_Mapping theNewProduct_Category_Mapping = new Data.Entity.Product_Category_Mapping();

					theNewProduct_Category_Mapping.CategoryID = currentCategoryID;
					theNewProduct_Category_Mapping.DisplayOrder = int.Parse(TextDisplayOrder.Text.Trim());
					theNewProduct_Category_Mapping.IsFeaturedProduct = false;
					theNewProduct_Category_Mapping.ProductID = ProductID;

					_ProductService.SaveProduct_Category_Mapping(theNewProduct_Category_Mapping);
				}
				else
				{
					if (lstProductCategories != null && lstProductCategories.Count() > 0)
					{
						if (lstProductCategories.Contains(currentCategoryID))
						{
							Data.Entity.Product_Category_Mapping theNewProduct_Category_Mapping = new Data.Entity.Product_Category_Mapping();

							theNewProduct_Category_Mapping.CategoryID = currentCategoryID;
							theNewProduct_Category_Mapping.DisplayOrder = int.Parse(TextDisplayOrder.Text.Trim());
							theNewProduct_Category_Mapping.IsFeaturedProduct = false;
							theNewProduct_Category_Mapping.ProductID = ProductID;

							_ProductService.DeleteProduct_Category_Mapping(theNewProduct_Category_Mapping);
						} // delete
					}
				}
			}

			return -1;
		}
	}
}